<?php
	include ('../defines.php');

	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'countries.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);
	
	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$news = $users->getLoggedUser();
	$countries = new Countries();
	
	if (!empty($_POST)){
		$arrCountry = array();
		unset($_POST['sbmSubmit']);
		
		foreach ($_POST as $key => $value)
			$arrCountry[$key] = $value;
		if(!empty($_FILES['flag']['name'])){
			$_FILES['flag']['name'] = $_FILES['flag']['name'];
			$arrCountry['flag'] = $_FILES['flag']['name'];
			$arrCountry['eliminar'] = 1;
			$arrCountry['files'] = $_FILES['flag'];
		}

		if ($_POST['action'] != 'a'){
			if ($countries->updateCountry($arrCountry))
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'countries.php');	
		}else if ($_POST['action'] == 'a'){
			if ($countries->createCountry($arrCountry)){
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'countries.php');
			}
		}		
	}
	
	if ($_GET['action'] != 'a'){
		$country = $countries->getCountry(
			array(
				'country_id' => $_GET['id']
			)
		);
	}else{
		$country = $countries->getCountry();
	}
	FunctionsHelper::includeTop();
?>
<div class="withBorder" id="main">
	<script type="text/javascript">
		$(function(){
			$('#back').click(function(){
				document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>countries.php';
			});

			$(function(){
				$('.form').validate();
			});
		});
	</script>
	<div class="mainABM">
		<div class="ABMtitle"><h1>ADMINISTRADOR DE PAISES</h1></div>
		<form id='frmCountry' class='form' method='POST' enctype="multipart/form-data">
			<input type='hidden' name='country_id' id='country_id' value='<?= $country->getCountryId(); ?>' />
			<input type='hidden' name='action' id='action' value='<?= $_GET['action']; ?>' />
			<div class="label">Pa&iacute;s:</div><input type='text' value='<?= $country->getName();?>' class='required tinyInput' name='name' id='name' />
			<br />
			<div class="label">Continente:</div> 
			<select id='continent' name='continent'>
				<option value='1' <?= ($country->getContinent() == 1) ? 'selected="selected"' : ''; ?> >Am&eacute;rica del sur</option>
				<option value='2' <?= ($country->getContinent() == 2) ? 'selected="selected"' : ''; ?> >Centro y Norte Am&eacute;rica</option>
				<option value='3' <?= ($country->getContinent() == 3) ? 'selected="selected"' : ''; ?> >&Aacute;frica</option>
				<option value='4' <?= ($country->getContinent() == 4) ? 'selected="selected"' : ''; ?> >Asia</option>
				<option value='5' <?= ($country->getContinent() == 5) ? 'selected="selected"' : ''; ?> >Europa</option>
				<option value='6' <?= ($country->getContinent() == 6) ? 'selected="selected"' : ''; ?> >Ocean&iacute;a</option>
			</select>
			<br />
			<div class="label">Bandera:</div> 
			<input type="file" name='flag' id='flag' />
			<br />
			<br />
			<?php 
			$flag = $country->getFlag();
			if (!empty($flag) && $_GET['action']!='a'){
				if (!file_exists(MAIN_IMAGES_FLAG_DIRECTORY . $flag))
					$flag = NO_IMAGE;
				else 
					$flag = MAIN_IMAGES_FLAG_PATH . utf8_encode($flag);

			?>
			<img src='<?= $flag; ?>' class="abmFlag" /> <input type="checkbox" name='eliminar' id='eliminar' value='1' /><label for="eliminar">Eliminar</label>
			<br />
			<?php 
			}
			?>
			<br />
			<input type="checkbox" name='active' id='active' value='1' <? echo $country->getActive() == 1 ? 'checked = "checked"' : ''; ?> /><label for="active">Activo</label>
			<br />
			<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Aceptar'  /></div>
		</form>
		<img src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width = '64' height = '52' class="back"/>
	</div>
</div>

<?php
	
	FunctionsHelper::includeBottom();
?>